/***********************************************************************
 * Module:  CommandManager.java
 * Author:  Nikola
 * Purpose: Defines the Class CommandManager
 ***********************************************************************/

package genedit.controller;

import genedit.gui.GMainFrame;
import genedit.model.Workspace;
import genedit.view.graph.GraphDocumentView;

import java.util.ArrayList;
import java.util.List;

public class CommandManager {
	private int currentCommandIndex = -1;

	private List<Command> commands = new ArrayList<Command>();

	/** @param command */
	public void executeCommand(Command command) {
		if (command.undoable) {
			for (int i = commands.size() - 1; i > currentCommandIndex; i--)
				commands.remove(i);
			addCommands(command);
			setCurrentCommandIndex(currentCommandIndex + 1);
			GraphDocumentView activeDocument=Workspace.getInstance().getActiveDocument();
			GMainFrame.getInstance().documentChanged(activeDocument.getModel());
		}
		command.execute();
	}

	public void undo() {
		if (currentCommandIndex > -1) {
			Command currentCommandToUndo = commands.get(currentCommandIndex);
			currentCommandToUndo.undo();
			setCurrentCommandIndex(currentCommandIndex - 1);
			//nema promena na dokumentu
			if(commands.size()==0){
				GraphDocumentView activeDocument=Workspace.getInstance().getActiveDocument();
				GMainFrame.getInstance().documentSaved(activeDocument.getModel());
			}
		}
	}

	public void redo() {
		if (currentCommandIndex < commands.size() - 1) {
			Command currentCommandToRedo = commands
					.get(currentCommandIndex + 1);
			currentCommandToRedo.execute();
			setCurrentCommandIndex(currentCommandIndex + 1);
			GraphDocumentView activeDocument=Workspace.getInstance().getActiveDocument();
			GMainFrame.getInstance().documentChanged(activeDocument.getModel());
		}
	}

	public int getCurrentCommandIndex() {
		return currentCommandIndex;
	}

	/** @param newCurrentCommandIndex */
	public void setCurrentCommandIndex(int newCurrentCommandIndex) {
		currentCommandIndex = newCurrentCommandIndex;
		if (currentCommandIndex > -1)
			GMainFrame.getInstance().documentChanged(
					Workspace.getInstance().getActiveDocument().getModel());
		else
			GMainFrame.getInstance().documentSaved(
					Workspace.getInstance().getActiveDocument().getModel());
	}

	/** @pdGenerated default getter */
	public List<Command> getCommands() {
		if (commands == null)
			commands = new ArrayList<Command>();
		return commands;
	}

	/** @pdGenerated default iterator getter */
	public java.util.Iterator<Command> getIteratorCommands() {
		if (commands == null)
			commands = new ArrayList<Command>();
		return commands.iterator();
	}

	/**
	 * @pdGenerated default setter
	 * @param newCommands
	 */
	public void setCommands(java.util.Collection<Command> newCommands) {
		removeAllCommands();
		for (java.util.Iterator<Command> iter = newCommands.iterator(); iter
				.hasNext();)
			addCommands((Command) iter.next());
	}

	/**
	 * @pdGenerated default add
	 * @param newCommand
	 */
	public void addCommands(Command newCommand) {
		if (newCommand == null)
			return;
		if (this.commands == null)
			this.commands = new ArrayList<Command>();
		if (!this.commands.contains(newCommand))
			this.commands.add(newCommand);
	}

	/**
	 * @pdGenerated default remove
	 * @param oldCommand
	 */
	public void removeCommands(Command oldCommand) {
		if (oldCommand == null)
			return;
		if (this.commands != null)
			if (this.commands.contains(oldCommand))
				this.commands.remove(oldCommand);
	}

	/** @pdGenerated default removeAll */
	public void removeAllCommands() {
		if (commands != null)
			commands.clear();
		setCurrentCommandIndex(-1);
	}

}